﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Belyn.Common.Web;
using Cartoon.Model;

namespace Cartoon.Logic
{
    public class CartoonDownloadManage:DownloadManage
    {

        private ICartoonDownloadView DV;

        public CartoonDownloadManage()
            : base()
        { 
        }

        public CartoonDownloadManage(ICartoonDownloadView _DV)
            : base()
        {
            this.DV = _DV;
        }

        public override void Add(IDownload dl)
        {
            if (this.Exists(dl) == false)
            {
                dl.Canceled += new DownloadDelegate(dl_Canceled);
                dl.Downloading += new DownloadDelegate(dl_Downloading); 
                dl.Stoped += new DownloadDelegate(dl_Stoped); 
            }
            
            base.Add(dl); 
        } 

        public override void Remove(IDownload dl)
        {
            base.Remove(dl);
            this.OnRemoved(dl);
        }

        public override bool Exists(IDownload dl)
        {
            return base.Exists(dl) || this.Get(dl.Title)!=null;
        }

        public IDownload Get(string title)
        {
            foreach (IDownload d in this.DownloadList)
            {
                if (d.Title == title)
                    return d;
            }

            return null;
        }

        private void dl_Stoped(IDownload d)
        { 
            this.Remove(d);
        } 

        private void dl_Downloading(IDownload d)
        { 
            if (d.Over == true)
            {
                this.Remove(d);
            } 
        }

        private void dl_Canceled(IDownload d)
        { 
            this.Remove(d);
        } 
        
    }
}
